$(function(){

    /*先隐藏*/
    $("#id1").hide();$("#id2").hide();$("#id3").hide();$("#id4").hide();
    /*再根据hotelRoomType 部分显示*/
    $.each(JSON.parse(hotelRoomType),function (index,value) {
        var num=value;
        console.info("num:"+num);
        $("#id"+num).show();
    })
    var mydate = new Date();
    var str = '"' + mydate.getFullYear() + "-";
    str += (mydate.getMonth()+1) + "-";
    str += mydate.getDate() + '"';
    console.info("当前时间："+str )
    var addDate = new Date(new Date(str).getTime() + (1000 * 60 * 60 * 24));
    var str1 = '"' + addDate.getFullYear() + "-";
    str1+= (addDate.getMonth()+1) + "-";
    str1 += addDate.getDate() + '"';
    console.info("第二天时间："+str1)

    $('#datetimepicker1').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn'),
        defaultDate: str
    });
    $('#datetimepicker2').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn'),
        defaultDate: str1
    });

    var picker1 = $('#datetimepicker1').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn')
    });
    var picker2 = $('#datetimepicker2').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn')
    });
    //动态设置最小值
    picker1.on('dp.change', function(e) {
        picker2.data('DateTimePicker').minDate(e.date);
       //计算两个日期的相差天数
        $("#nights").empty();
        var date1=$("#timeIn").val();
        var date2=$("#timeOut").val();
        date1=date1.replace(/-/g,"/");
        date2=date2.replace(/-/g,"/");
        var startdate=new Date(date1);
        var enddate=new Date(date2);
        var time=enddate.getTime()-startdate.getTime();
        var days=parseInt(time/(1000 * 60 * 60 * 24));
        $("#nights").val(days)
        console.info("date"+date1+"--------"+date2+"-------------days:"+days)
    });
    //动态设置最大值
    picker2.on('dp.change', function(e) {
        picker1.data('DateTimePicker').maxDate(e.date);
        //计算两个日期的相差天数
        $("#nights").empty();
        var date1=$("#timeIn").val();
        var date2=$("#timeOut").val();
        date1=date1.replace(/-/g,"/");
        date2=date2.replace(/-/g,"/");
        var startdate=new Date(date1);
        var enddate=new Date(date2);
        var time=enddate.getTime()-startdate.getTime();
        var days=parseInt(time/(1000 * 60 * 60 * 24));
        $("#nights").val(days)
        console.info("date"+date1+"--------"+date2+"-------------days:"+days)
    });

    $("#updateBtn").click(function () {
        $("#reservePri").empty();      //表头预约价格清空    html完全替换   append 追加   val设置value值
        var date1=$("#timeIn").val();
        var date2=$("#timeOut").val();
        var days=$("#nights").val();
        var  bigHomePriNew=bigHomePri*days;
        var  douHomePriNew=douHomePri*days
        var  circleHomePriNew=circleHomePri*days
        var  couplesHomePriNew=couplesHomePri*days
        setValue(bigHomePriNew,douHomePriNew,circleHomePriNew,couplesHomePriNew)
        $("#bigHomePri").html("RMB "+bigHomePriNew)
        $("#douHomePri").html("RMB "+douHomePriNew)
        $("#circleHomePri").html("RMB "+circleHomePriNew)
        $("#couplesHomePri").html("RMB "+couplesHomePriNew)
        $("#reservePri").append("预约总价");       //表头内容追加
        console.info("天数:"+days+"---"+"房间价格:"+bigHomePriNew+"--"+douHomePriNew+"---"+circleHomePriNew+"--"+couplesHomePriNew)

    })
    /*$("#bookRoom").on("click","tr",function () {
        var data = new Array();
        var td = $(this).find("td");
        for (var i = 0; i < td.length; i++) {
            data.push(td.eq(i).text());
        }
        data.push(td.eq(i).text());
        alert("行数据:"+data);
    })*/
    //返回酒店列表
    $('#returnHome').click(function () {
        window.location.href=ctx+"hotellist/listpage";
    })
});

/*确认修改后预约价格变化 预约传值也要变化*/
function setValue(num1,num2,num3,num4) {
    $('#bigHomePri').val(num1);
    $('#douHomePri').val(num2);
    $('#circleHomePri').val(num3);
    $('#couplesHomePri').val(num4);
}

function bookRoom(id,name,num){  /*id num 主要是修改hotel表数据  name pri预约页面*/
        var resultJson={};
       //可以序列化表单啊 这不就可以少些几个了吗 蠢死我了
        resultJson.inTime=$('#timeIn').val();
        resultJson.outTime=$('#timeOut').val();
        resultJson.rooms='1';
        resultJson.peoples=$('#peoples').val();
        resultJson.nights=$('#nights').val();
        resultJson.hotelId=id;
        var hotelRoom="";
        var hotel=""
        debugger;
        if (num == 1){
          hotelRoom='大床房';
          if ($('#bigHomePri').val()!='' && $('#bigHomePri').val()!=null)
          {
              hotel=$('#bigHomePri').val();
          }else {
              hotel=bigHomePri
          }
        }else if (num == 2){
           hotelRoom='双人房';
           if ($('#douHomePri').val()!='' && $('#douHomePri').val()!=null){
               hotel=$('#douHomePri').val();
           }else {
               hotel=douHomePri
           }
        }
        else if (num == 3){
            hotelRoom='商务房';
            if ($('#circleHomePri').val()!='' && $('#circleHomePri').val()!=null){
                hotel=$('#circleHomePri').val();
            }else {
                hotel=circleHomePri
            }

        }else{
           hotelRoom='情侣房';
           if ($('#couplesHomePri').val()!='' && $('#couplesHomePri').val()!=null){
               hotel=$('#couplesHomePri').val();
           }else{
               hotel=couplesHomePri
           }
        }
        resultJson.pri=hotel;                            /*这个地方的价格是原始价格  正确价格:原始价格*折扣*/
        resultJson.hotelRoom=num;                       //房型num
        resultJson.hotelNameRoom=name+"-"+hotelRoom    //酒店名字+酒店房型
        var hotelNameRoom=name+"-"+hotelRoom;
        var pri=hotel
        $.ajax({
            url:ctx+"hotellist/BookhotelInfo",
            type:"post",
            data:resultJson,
            dataType:"json",
            success : function (data) {
                if(data.msg!='操作失败'){
                    var msg=data.msg;//商品订单号
                    console.info("创建订单号:"+msg);
                    window.location.href=
                        ctx+"hotellist/hotelPay?inTime="+$('#timeIn').val()+
                        "&outTime="+$('#timeOut').val()+
                        "&hotelNameRoom="+hotelNameRoom+
                        "&hotelPri="+pri+
                        "&hotelId="+id+
                        "&trade_out="+msg+
                        "&hotelRoom="+num;
                }
            }
        })

        console.info("Result:"+resultJson);
}

/**/
function removeClassType($this) {
    $($this).attr("title",$this.innerText);
}

